package com.blb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.*;
import com.blb.mapper.IoWarehouseMapper;
import com.blb.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-16
 */
@Service
public class IoWarehouseServiceImpl extends ServiceImpl<IoWarehouseMapper, IoWarehouse> implements IIoWarehouseService {

    @Resource
    private IoWarehouseMapper ioWarehouseMapper;
    @Resource
    private IProductHandoverService pHandoverService;
    @Resource
    private ICargoService cargoService;
    @Resource
    private IOrderService orderService;
    @Resource
    private ICargoAreaService iCargoAreaService;
    @Resource
    private IShelvesService shelvesService;
    @Resource
    private IIomWarehouseService iomWarehouseService;
    @Resource
    private IProductiveProcessService productiveProcessService;

    @Override
    public Integer getBatch() {
        return ioWarehouseMapper.getBatch();
    }

    @Override
    public List<Integer> getAllBatch(String companyId) {
        return ioWarehouseMapper.getAllBatch(companyId);
    }

    @Override
    public IoWarehouse getPNameByBatch(String outBatchNo) {
        return ioWarehouseMapper.getPNameByBatch(outBatchNo);
    }

    @Override
    public List<Map<String, Object>> getPNameAndCode(String orderId) {
        return ioWarehouseMapper.getPNameAndCode(orderId);
    }

    @Override
    public int countIn(String orderId) {
        return ioWarehouseMapper.countIn(orderId);
    }

    @Override
    public Map<String, Object> getPByOrderId(String orderId) {
        return ioWarehouseMapper.getPByOrderId(orderId);
    }

    @Transactional
    @Override
    public void addInWarehouse(IomDetailList iomDetails, IoWarehouse inW, String handoverId, String pNo,String oNo) {
        inW.setId(UUIDUtils.getId());
        for (IomWarehouse iom : iomDetails.getIomDetails()) {
            iom.setId(UUIDUtils.getId());
            iom.setIomTime(inW.getIoTime());
            iom.setIomNo(inW.getIoNo());
        }
        save(inW);
        pHandoverService.updateExt(handoverId);
        IomDetailList iomDetailList = iomDetails;
        for (IomWarehouse iom : iomDetails.getIomDetails()) {
            Cargo cargo = new Cargo();
            cargo.setId(iom.getIomCargoloca());
            cargo.setBatch(inW.getBatch());
            cargo.setLCode(iom.getIomGoodsno());
            cargo.setProduct(iom.getIomGoods());
            cargo.setProductNo(pNo);
            cargo.setStatus("有货");
            cargo.setIomNo(iom.getIomNo());
            cargoService.updateById(cargo);
        }
        for (IomWarehouse iom : iomDetailList.getIomDetails()) {
            Cargo c = cargoService.getById(iom.getIomCargoloca());
            iom.setIomCargoloca(c.getCargoNo());
            Shelves s = shelvesService.getById(iom.getIomShelves());
            iom.setIomShelves(s.getShelvesNo());
            CargoArea a = iCargoAreaService.getById(iom.getIomArea());
            iom.setIomArea(a.getGoodsName());
            iomWarehouseService.save(iom);
        }
        Order order = orderService.getOne(new QueryWrapper<Order>().eq("order_id", oNo));
        order.setProductStatus("9");
        productiveProcessService.insertP(oNo,9);
        orderService.updateById(order);
    }


    @Transactional
    @Override
    public void addOutWarehouse(IoWarehouse outW) {
        IoWarehouse inWarehouse = getOne(new QueryWrapper<IoWarehouse>().eq("batch", outW.getBatch()));
        List<IomWarehouse> iomWarehouseList = iomWarehouseService.list(new QueryWrapper<IomWarehouse>().eq("iom_no",inWarehouse.getIoNo()));
        for (IomWarehouse outWarehouse : iomWarehouseList) {
            outWarehouse.setId(UUIDUtils.getId());
            outWarehouse.setIomTime(outW.getIoTime());
            outWarehouse.setIomNo(outW.getIoNo());
            iomWarehouseService.save(outWarehouse);
        }
        List<Cargo> cargoList = cargoService.list(new QueryWrapper<Cargo>().eq("batch", outW.getBatch()));
        for (Cargo cargo : cargoList) {
            ioWarehouseMapper.updateCargoNull(cargo.getId());
        }
        outW.setId(UUIDUtils.getId());
        outW.setWName(iomWarehouseList.get(0).getIomName());
        save(outW);
    }
}
